//
//  ViewController.m
//  照片墙
//
//  Created by 融易科技 on 2017/4/27.
//  Copyright © 2017年 糊糊糊. All rights reserved.
//

#import "ViewController.h"
#import "imageShowVC.h"
@interface ViewController ()
@property (nonatomic, strong)UIScrollView *sc;
@property (nonatomic, strong)NSArray *picArray;
@property (nonatomic, strong)NSMutableArray *viewArray;
@end

#define gscTag 100000
@implementation ViewController
-(NSMutableArray *)viewArray{
    if (!_viewArray) {
        _viewArray = [NSMutableArray new];
    }
    return _viewArray;
}

-(NSArray *)picArray{
    if (!_picArray) {
          _picArray = [NSArray arrayWithObjects:@"引导页3",@"图层-1---Assistor.jpg",@"引导页2",@"引导页4",@"图层-1---Assistor.jpg",@"引导页2",@"引导页3",@"图层-1---Assistor.jpg",@"引导页4",@"图层-1---Assistor.jpg", nil];
    }
    return _picArray;
}
-(UIScrollView *)sc{
    if (!_sc) {
        _sc = [[UIScrollView alloc] init];
        _sc.contentSize = CGSizeMake(self.view.frame.size.width,self.view.frame.size.height * 1.5);
        _sc.userInteractionEnabled = YES;
        _sc.showsVerticalScrollIndicator = NO;
    }
    return _sc;
}

-(void)loadView{
    [super loadView];
    [self.view addSubview:self.sc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUIViewMask];
}

- (void)setUIViewMask{
      [_sc mas_makeConstraints:^(MASConstraintMaker *make) {
          make.edges.equalTo(self.view);
      }];

    for (int i = 0; i < self.picArray.count; i++) {
        UIImage *image = [UIImage imageNamed:self.picArray[i]];
        UIImageView *imageView =[[UIImageView alloc] initWithImage:image];
        int Y = (i/4) * 140 ;
        int X =  i % 4 * 100;
        imageView.frame = CGRectMake(X, Y, 90, 130);
        imageView.tag = gscTag + i;
        imageView.userInteractionEnabled = YES;
        [self.viewArray addObject:imageView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [imageView addGestureRecognizer:tap];
        [self.sc addSubview:imageView];
    }
}

- (void)tapAction:(UITapGestureRecognizer *)tap{

    UIImageView *imageView = (UIImageView *)tap.view;
   __block CGRect rect = imageView.frame;
    imageShowVC *show = [[imageShowVC alloc] init];
    /**
     *注意我用的这种传值imageView的方法 会导致你的本页面的imageView消失
     * 因此才有下面的滑动触发的block;
     **/
    show.imageView = imageView;
    show.displayIndex = (int)imageView.tag - gscTag;
    show.picArray = self.picArray;
    
    show.popTwoB = ^(UIView *view, UIImageView *smallView) {
            //删除上一级的父视图
            [view removeFromSuperview];
            [UIView animateWithDuration:0.5 animations:^{
                smallView.frame = rect;
            }];
            [self packAction:smallView];
    };
    
    show.popB = ^(UIView *view,UIImageView *bigView) {
            //删除上一级的父视图
           [view removeFromSuperview];
            UIImageView *imageVVV = [self.viewArray objectAtIndex:bigView.tag - gscTag];
            [UIView animateWithDuration:0.5 animations:^{
                bigView.frame = imageVVV.frame;
                
            }];
         [self packAction:bigView];
    };
   /*触发这个方法-->代表滑动了,不是点击时候的那张图片了。
    *将原来的小图再从新添加到页面上
    */
    show.scrollB = ^(UIImageView *imageV) {
        imageV.frame = rect;
        [self packAction:imageV];
    };
    [self.view addSubview:show.view];
    [self addChildViewController:show];
}
 
- (void)packAction:(UIView *)view{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    view.userInteractionEnabled = YES;
    [view addGestureRecognizer:tap];
    [self.sc addSubview: view];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
